package com.wang.users.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wang.common.service.BaseService;
import com.wang.users.pojo.AStatRes;
import com.wang.users.pojo.AStatResEnum;
import com.wang.users.service.IUsersOperService;

/**
 * 
 * @author lby
 *
 */
public class UsersOperServiceImpl extends BaseService implements IUsersOperService
{
	private  UsersDbServiceImpl usersDbService = new UsersDbServiceImpl();
	
	@Override
	public JSONObject saveOneStatResService(Date currDate)
	{
		JSONObject resultDbJSON = new JSONObject();
		Map<String, Object> condMap = new HashMap<String,Object>();
		
		/*查询指定时间的数据*/
		Calendar calendar = Calendar.getInstance();
		/*添加查询时间*/
		calendar.setTime(currDate);
		/*表调成零点*/
		/*设置时*/
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		/*设置分钟*/
		calendar.set(Calendar.MINUTE, 0);
		/*设置秒*/
		calendar.set(Calendar.SECOND, 0);
		
		/*计算时间范围*/
		/*设置结束时间*/
		Date edDate = calendar.getTime();
		calendar.add(Calendar.DATE,-1);
		/*设置开始时间*/
		Date stDate = calendar.getTime();	
		
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		
		condMap.put("operType", "dataStat");
		
		/*这个数据，条件已经限定死了，只能是一条*/
		List<Map<String, Object>> resultList = this.usersDbService.statLogListService(null, condMap);
		Map<String, Object> dataMapTemp = resultList.get(0);
		
		AStatRes statRes = new AStatRes();
		statRes.setCurrDate(stDate);
		/*他是一个JSON*/
		/*设置一个统计日期：是自己传过去的时间的前一天*/
		statRes.setCurrDate(stDate);
		
		/*获取存储的数据*/
		statRes.setUsersData(JSON.toJSONString(dataMapTemp));
		/*设置状态*/
		statRes.setStatus(AStatResEnum.STATUS_ENABLED.getCode());
		/*保存一条数据,返回结果*/
		resultDbJSON = this.usersDbService.saveOneStatResService(statRes);
		return resultDbJSON;
	}
	
}
